package com.qyhl.cloud.webtv.module_integral.common;

import com.alibaba.android.arouter.launcher.ARouter;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.AppConfigConstant;
import com.qyhl.webtv.commonlib.constant.PathConfigConstant;
import com.qyhl.webtv.commonlib.entity.intergral.CoinBean;
import com.qyhl.webtv.commonlib.entity.intergral.ExchangerRateBean;
import com.qyhl.webtv.commonlib.entity.intergral.IntegralUserInfoBean;
import com.qyhl.webtv.commonlib.entity.user.LuckDrawAdvBean;
import com.qyhl.webtv.commonlib.service.IntergralService;
import com.qyhl.webtv.commonlib.utils.config.AppConfigUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;

/**
 * 积分工具类
 *
 * @author helin
 * @date 2019年3月28日18:19:05
 */
public class IntergralUtils {


    private static IntergralUtils intergralUtils;

    public static IntergralUtils getInstance() {
        if (intergralUtils == null) {
            intergralUtils = new IntergralUtils();
        }
        return intergralUtils;
    }

    private IntergralUtils() {
        ARouter.getInstance().inject(this);
    }


    /**
     * 获取汇率信息
     *
     * @param callBack
     */
    public void getExchangerRate(IntergralService.ExchangerRate callBack) {
        Map<String, String> map = DESedeUtil.encodeMapUrl(IntergralUrl.INTEGRAL_URL);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("name", "coinRate");
            jsonObject.put("version", PathConfigConstant.version);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY),
                        jsonObject.toString()
                ))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<ExchangerRateBean>() {
                    @Override
                    public void onError(ApiException e) {
                        callBack.error(e.getMsg());
                    }

                    @Override
                    public void onSuccess(ExchangerRateBean exchangerRateBean) {
                        callBack.success(exchangerRateBean);
                    }
                });


    }

    /**
     * 获取抽奖信息
     *
     * @param callBack
     */
    public void getLuckDraw(IntergralService.LuckDraw callBack) {
        Map<String, String> map = DESedeUtil.encodeMapUrl(IntergralUrl.INTEGRAL_URL);
        JSONObject object = new JSONObject();
        try {
            object.put("siteId", CommonUtils.getInstance().getSite());
            object.put("version", "1");
            object.put("name", "queryLuckyAct");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), object.toString()))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<List<LuckDrawAdvBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        callBack.error(e.getMsg());
                    }

                    @Override
                    public void onSuccess(List<LuckDrawAdvBean> luckDrawAdvBeans) {
                        callBack.success(luckDrawAdvBeans);
                    }
                });


    }

    /**
     * 获取签到信息
     *
     * @param callBack
     */
    public void getSignInfo(IntergralService.SignInCallBack callBack) {

        Map<String, String> map = DESedeUtil.encodeMapUrl(IntergralUrl.INTEGRAL_URL);
        JSONObject jsonObject = new JSONObject();
        String userName = CommonUtils.getInstance().getUserName();
        try {
            jsonObject.put("name", "userInfo");
            jsonObject.put("version", PathConfigConstant.version);
            jsonObject.put("username", userName);
            jsonObject.put("siteId", CommonUtils.getInstance().getSite());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY),
                        jsonObject.toString()
                ))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<IntegralUserInfoBean>() {
                    @Override
                    public void onError(ApiException e) {
                        callBack.error(e.getMsg());
                    }

                    @Override
                    public void onSuccess(IntegralUserInfoBean integralUserInfoBean) {
                        callBack.success(integralUserInfoBean);
                    }
                });

    }


    /**
     * 获取金币
     *
     * @param type         gainCoin  readNews
     * @param coinCallBack
     */
    public void getCoin(String type, String name, IntergralService.GainCoinCallBack coinCallBack) {

        Map<String, String> map = DESedeUtil.encodeMapUrl(IntergralUrl.INTEGRAL_URL);
        JSONObject object = new JSONObject();
        try {
            object.put("siteId", CommonUtils.getInstance().getSite());
            object.put("version", PathConfigConstant.version);
            object.put("name", name);
            object.put("username", AppConfigUtil.getInstance().getConValue(AppConfigConstant.USER_NAME));
            object.put("type", type);
        } catch (JSONException e) {
            e.printStackTrace();
        }


        EasyHttp.post(map.get(DESedeUtil.URL))
                .upBytes(DESedeUtil.encryptMode(map.get(DESedeUtil.CODEKEY), object.toString()))
                .isDecrypt(true)
                .decryptKet(map.get(DESedeUtil.CODEKEY))
                .execute(new SimpleCallBack<CoinBean>() {
                    @Override
                    public void onError(ApiException e) {
                        coinCallBack.error(getScoreMessage(e.getCode()));
                    }

                    @Override
                    public void onSuccess(CoinBean coinBean) {
                        coinCallBack.success(coinBean);
                    }
                });
    }


    /**
     * 积分错误码
     *
     * @param code 错误码
     * @return 错误信息
     */
    public String getScoreMessage(int code) {
        switch (code) {
            case 1:
                return "请求出错！";
            case 2:
                return "code:2,请求出错！";
            case 3:
                return "请求失败，参数异常！";
            case 4:
                return "请求失败，地址不存在！";
            case 5:
                return "code:5,请求出错！";
            case 11:
                return "操作异常！";
            case 12:
                return "今天已经签到！";
            case 13:
                return "金币达到上限！";
            case 14:
                return "行为不存在！";
            case 15:
                return "用户不存在！";
            case 21:
                return "金币不足！";
            case 22:
                return "金币太少！";
            case 31:
                return "未绑定支付宝账号！";
            case 32:
                return "未绑定微信账号！";
            case 33:
                return "提现余额不足！";
            case 34:
                return "提现手续费不足！";
            case 35:
                return "提现金额不在范围内！";
            case 36:
                return "未知的支付平台！";
            default:
                return "请求出错！";
        }
    }


}
